#include <Engine/StdAfx.h>
#include <Engine/IEngine.h>
#include <Engine/Foundation/Timer.h>

using namespace fastbird;

ITimer* ITimer::CreateTimer()
{
	return new Timer();
}

Timer::Timer()
{
	bool support = QueryPerformanceFrequency(&mFreq)!=0;
	if(!support)
	{
		mFreq.QuadPart = 0;
		IEngine::Log(FB_DEFAULT_DEBUG_ARG, "PerformanceCounter is not supported.");
	}
	Reset();
}

Timer::~Timer()
{
}

void Timer::Tick()
{
	float previousTime = mTime;
	mTime = GetTickCount() / (TIME_PRECISION)mFreq.QuadPart - mStartTime;
	mDeltaTime = mTime - previousTime;
}

ITimer::TIME_PRECISION Timer::GetDeltaTime()
{
	return mDeltaTime;
}

ITimer::TIME_PRECISION Timer::GetTime()
{
	return mTime;
}

void Timer::Reset()
{
	QueryPerformanceCounter( &mBase );
	mStartTime = mBase.QuadPart / (TIME_PRECISION)mFreq.QuadPart;
	mTime = 0;
	mDeltaTime = 0;
}

__int64 Timer::GetTickCount()
{
	LARGE_INTEGER newCount;
	QueryPerformanceCounter(&newCount);
	return newCount.QuadPart;
}

//----------------------------------------------------------------------------
inline __int64 Timer::GetFreq() const
{
	return mFreq.QuadPart;
}